package com.fiberhome.boot.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

public class RedisUtils {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	StringRedisTemplate stringRedisTemplate;
	
	RedisTemplate<String, Object> objectRedisTemplate;
	
	public RedisUtils(StringRedisTemplate stringRedisTemplate){
		this.stringRedisTemplate = stringRedisTemplate;
	}
	
	public RedisUtils(RedisTemplate<String, Object> objectRedisTemplate){
		this.objectRedisTemplate = objectRedisTemplate;
	}
	
	public RedisUtils(StringRedisTemplate stringRedisTemplate,RedisTemplate<String, Object> objectRedisTemplate){
		this.stringRedisTemplate = stringRedisTemplate;
		this.objectRedisTemplate = objectRedisTemplate;
	}
	
	public String getStringValue(String key){
		if(objectRedisTemplate==null){
			logger.error("objectRedisTemplate is null");
			return null;
		}
		logger.info("redis get value  : key ------ "+key);
		return stringRedisTemplate.opsForValue().get(key);
	}
	
	public Object getObjectValue(String key){
		if(objectRedisTemplate==null){
			logger.error("objectRedisTemplate is null");
			return null;
		}
		logger.info("redis get value  : key ------ "+key);
		return objectRedisTemplate.opsForValue().get(key);
	} 
	
	public void setStringValue(String key, String value){
		if(stringRedisTemplate==null){
			logger.error("stringRedisTemplate is null");
			return ;
		}
		logger.info("redis save string value : key ------ "+key+" value ----"+value);
		stringRedisTemplate.opsForValue().set(key, value);
	}
	
	public void setObjectValue(String key, Object obj){
		if(objectRedisTemplate==null){
			logger.error("objectRedisTemplate is null");
			return ;
		}
		logger.info("redis save object value : key ------ "+key+" value ----"+obj);
		objectRedisTemplate.opsForValue().set(key, obj);
	}
	
	public void deleteStringValue(String key){
		if(stringRedisTemplate==null){
			logger.error("stringRedisTemplate is null");
			return ;
		}
		logger.info("redis delete value  : key ------ "+key);
		stringRedisTemplate.delete(key);
	}
	
	
}
